package com.sls.web.manager.recharge.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.Supplier;
import com.sls.web.manager.recharge.pojo.SupplierRechargeMode;
import com.sls.web.manager.recharge.querybean.SupplierRechargeModeQuery;
import com.sls.web.manager.recharge.service.SupplierRechargeModeService;
import com.sls.web.manager.socket.SuperService;

@Service("supplierRechargeModeService")
public class SupplierRechargeModeServiceImpl extends SuperService implements
		SupplierRechargeModeService {

	@Override
	public boolean save(SupplierRechargeMode sa, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.INSERT_SUPPLIER_RECHARGE_MODE);
		if (sa.getEnableQuery() != null)
			sa.setEnableQuery(true);
		else
			sa.setEnableQuery(false);
		if (sa.getEnableRecharge() != null)
			sa.setEnableRecharge(true);
		else
			sa.setEnableRecharge(false);
		if (sa.getEnableReverse() != null)
			sa.setEnableReverse(true);
		else
			sa.setEnableReverse(false);
		if (sa.getEnableAdjustment() != null)
			sa.setEnableAdjustment(true);
		else
			sa.setEnableAdjustment(false);
		if (sa.getEnablePointRecharge() != null)
			sa.setEnablePointRecharge(true);
		else
			sa.setEnablePointRecharge(false);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, sa);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());

	}

	@Override
	public Pagination<?> queryList(SupplierRechargeModeQuery rQuery,
			UserInfoVO userinfo) throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_RECHARGE_MODE_LIST);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, rQuery);
		String body = dto.getBody();
		Pagination<SupplierRechargeMode> s = gson.fromJson(body,
				new TypeToken<Pagination<SupplierRechargeMode>>() {
				}.getType());

		return s;
	}

	@Override
	public boolean delete(String id, UserInfoVO userinfo) throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_SUPPLIER_RECHARGE_MODE);
		SupplierRechargeMode su = new SupplierRechargeMode();
		su.setId(id);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, su);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public SupplierRechargeMode queryById(String id, UserInfoVO userinfo)
			throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_SUPPLIER_RECHARGE_MODE_BY_ID);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SupplierRechargeMode sa = new SupplierRechargeMode();
		sa.setId(id);
		SocketDTO dto = super.send(head, sa);
		String body = dto.getBody();
		SupplierRechargeMode s = gson
				.fromJson(body, SupplierRechargeMode.class);
		return s;
	}

	@Override
	public boolean update(SupplierRechargeMode sa, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.UPDATE_SUPPLIER_RECHARGE_MODE);
		if (sa.getEnableQuery() != null)
			sa.setEnableQuery(true);
		else
			sa.setEnableQuery(false);
		if (sa.getEnableRecharge() != null)
			sa.setEnableRecharge(true);
		else
			sa.setEnableRecharge(false);
		if (sa.getEnableReverse() != null)
			sa.setEnableReverse(true);
		else
			sa.setEnableReverse(false);
		if (sa.getEnableAdjustment() != null)
			sa.setEnableAdjustment(true);
		else
			sa.setEnableAdjustment(false);
		if (sa.getEnablePointRecharge() != null)
			sa.setEnablePointRecharge(true);
		else
			sa.setEnablePointRecharge(false);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, sa);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public List<Supplier> querySupplier(UserInfoVO userinfo) {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setServiceCode(Constants.QUERY_SUPPLIER);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head);
		List<Supplier> saList = gson.fromJson(dto.getBody(),
				new TypeToken<List<Supplier>>() {
				}.getType());

		return saList;
	}
}
